#include <cstdio>
typedef long long ll;
const int N = 200005;
int n, p, fac[N];

inline ll qpow(ll base, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1) {
      (res *= base) %= p;
    }
    exp >>= 1;
    (base *= base) %= p;
  }
  return res;
}

inline ll C(ll a, ll b) {
  if (a > b) return 0;
  return fac[b] * qpow(fac[a], p - 2) % p * qpow(fac[b - a], p - 2) % p;
}

ll lucas(ll a, ll b) {
  if (!a) return 1;
  return C(a % p, b % p) * lucas(a / p, b / p) % p;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("Lucas.in", "r", stdin);
  freopen("Lucas.out", "w", stdout);
#endif
#endif
  int t;
  scanf("%d", &t);
  fac[0] = 1;
  while (t--) {
    int a, b;
    scanf("%d%d%d", &a, &b, &p);
    for (int i = 1; i <= a + b; ++i) {
      fac[i] = 1ll * fac[i - 1] * i % p;
    }
    printf("%lld\n", lucas(a, a + b));
  }
  return 0;
}